package leetcode;

public class ConvertNumberToHexadecimal {

	public static void main(String[] args) {
		ConvertNumberToHexadecimal object = new ConvertNumberToHexadecimal();
		System.out.println(object.toHex(-16));
	}
	
	public String toHex(int num) {
		StringBuilder sb = new StringBuilder();
		if (num == 0) {
			return sb.append(0).toString();
		}
		int count = 0;
		int fourBitSum = 0;
		char[] hx = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
				'c', 'd', 'e', 'f' };
		while (num != 0) {
			fourBitSum = fourBitSum + ((num & 1) << count);
			System.out.println("fourBitSum: " + fourBitSum + " count: " + count);
			count++;
			num >>>= 1;
			if (count == 4) {
				count = 0;
				sb.append(hx[fourBitSum]);
				fourBitSum = 0;
			}
		}
		if (fourBitSum != 0) {
			sb.append(hx[fourBitSum]);
		}
		return sb.reverse().toString();
	}
}
